package com.jintian.smart.kernel.core.expr;

import com.jintian.smart.kernel.core.ExprFunction;
import com.jintian.smart.kernel.core.annotation.expr.ExprFn;
import com.jintian.smart.kernel.core.annotation.expr.ExprFns;

import java.lang.reflect.Method;
import java.util.*;

public interface IFunctionRepository {
    Collection<ExprFunction> getFunctions();

    Map<String, Method> getNamedMethod();

    Set<String> registerStaticMethod(Class<?> clazz);

    Set<String> registerMethod(String fnName, Method method, Object target);

    default List<String> getExprFnName(Method method) {
        List<String> ret = new ArrayList<>();
        ExprFn fn1 = method.getDeclaringClass().getAnnotation(ExprFn.class);
        ExprFns fns = method.getAnnotation(ExprFns.class);
        List<ExprFn> fn2List = fns != null ? new ArrayList<>(Arrays.asList(fns.value())) : new ArrayList<>();
        {
            ExprFn fn2 = method.getAnnotation(ExprFn.class);
            if (fn2 != null) {
                fn2List.add(fn2);
            }
        }
        String namespace = fn1 != null && (fn1.code() != null && fn1.code().trim().length() > 0) ? (fn1.code() + ".")
                : "";
        if (fn2List.size() == 0) {
            ret.add(namespace + method.getName());
        } else {
            for (ExprFn fn2 : fn2List) {
                ret.add(namespace + fn2.code());
            }
        }
        return ret;
    }
}
